package mysql

import (
	"fresh-box-manage/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(50);not null;comment:'名称'"`
	Price   float64 `gorm:"type:decimal(10,2);not null;comment:'价格'"`
	Num     int     `gorm:"type:int(11);not null;comment:'数量'"`
	ClassId int     `gorm:"type:int(11);not null;comment:'分类ID'"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FreshFindById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(f).Error
}

func (f *Fresh) FreshUpdate(db *gorm.DB) error {
	return db.Debug().Updates(f).Error
}

func (f *Fresh) FreshDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(f).Error
}

func FreshList(db *gorm.DB, in *freshrpc.FreshListRequest) ([]*freshrpc.FreshList, error) {
	var list []*freshrpc.FreshList

	err2 := db.Debug().Model(&Fresh{}).
		Select(
			"id as fresh_id",
			"name",
			"price",
			"num").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list).
		Error
	return list, err2
}
